package com.csust.automaticteach.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.errorpractice.ErrorPracticeAddRequest;
import com.csust.automaticteach.model.dto.errorpractice.ErrorPracticeQueryRequest;
import com.csust.automaticteach.model.dto.errorpractice.ErrorPracticeUpdateRequest;
import com.csust.automaticteach.model.entity.ErrorPractice;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.vo.errorpratice.ErrorPracticeVO;
import com.csust.automaticteach.service.ErrorPracticeService;
import com.csust.automaticteach.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassNameErrorPracticeController
 * @Description 错题集控制器
 * @Author钰玟
 * @Date2025/7/12 下午2:34
 * @Version 1.0
 **/
@RestController
@RequestMapping("/errorPractice")
public class ErrorPracticeController {
    @Resource
    private ErrorPracticeService errorPracticeService;
    @Resource
    private UserService userService;

    /**
     * 添加错题本
     *
     * @param errorPracticeAddRequest 添加的错题本
     * @return 添加结果
     */
    @PostMapping("/add")
    public BaseResponse<Boolean> addErrorPractice(@RequestBody ErrorPracticeAddRequest errorPracticeAddRequest, HttpServletRequest request) {
        //校验参数
        ThrowUtils.throwIf(errorPracticeAddRequest == null, ErrorCode.PARAMS_ERROR);
        ErrorPractice errorPractice = new ErrorPractice();
        User loginUser = userService.getLoginUser(request);
        //拷贝属性
        BeanUtil.copyProperties(errorPracticeAddRequest, errorPractice);

        //设置额外的属性 1.浏览次数 2.用户id 3.json化的options
        errorPractice.setReviewCount(1);
        errorPractice.setUserId(loginUser.getId());
        List<String> options = errorPracticeAddRequest.getOptions();
        String jsonOptions = JSONUtil.toJsonStr(options);
        errorPractice.setOptions(jsonOptions);
        boolean b = errorPracticeService.saveOrUpdate(errorPractice);
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 分页获取错题本
     *
     * @param errorPracticeQueryRequest 添加的错题本
     * @return 添加结果
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<ErrorPracticeVO>> listErrorPracticeVOByPage(@RequestBody ErrorPracticeQueryRequest errorPracticeQueryRequest
            , HttpServletRequest request) {
        //校验参数
        ThrowUtils.throwIf(errorPracticeQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int current = errorPracticeQueryRequest.getCurrent();
        int pageSize = errorPracticeQueryRequest.getPageSize();
        User loginUser = userService.getLoginUser(request);
        errorPracticeQueryRequest.setUserId(loginUser.getId());
        Page<ErrorPractice> errorPracticePage = errorPracticeService.page(new Page<>(current, pageSize)
                , errorPracticeService.getErrorPracticeQueryWrapper(errorPracticeQueryRequest));
        Page<ErrorPracticeVO> errorPracticeVOPage = new Page<>(current, pageSize, errorPracticePage.getTotal());
        errorPracticeVOPage.setRecords(errorPracticeService.getErrorPracticeVOList(errorPracticePage.getRecords()));
        return ResultUtils.success(errorPracticeVOPage);
    }


    /**
     * 删除错题本中的一个题
     *
     * @param deleteRequest dto
     * @return 删除结果
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteErrorPractice(@RequestBody DeleteRequest deleteRequest) {
        //校验参数
        ThrowUtils.throwIf(deleteRequest == null, ErrorCode.PARAMS_ERROR);
        boolean b = errorPracticeService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }


    /**
     * 修改错题本中的一个题的相关状态
     *
     * @param errorPracticeUpdateRequest dto
     * @return 修改结果
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateErrorPractice(@RequestBody ErrorPracticeUpdateRequest errorPracticeUpdateRequest) {
        //校验参数
        ThrowUtils.throwIf(errorPracticeUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        //得到相关参数
        List<Long> idList = errorPracticeUpdateRequest.getIdList();
        List<Integer> reviewCountList = errorPracticeUpdateRequest.getReviewCountList();
        //赋值
        List<ErrorPractice> errorPracticeList = idList.stream().map(id -> {
            ErrorPractice errorPractice = new ErrorPractice();
            errorPractice.setId(id);
            return errorPractice;
        }).toList();
        //设置修改的属性
        for (ErrorPractice errorPractice : errorPracticeList) {
            int updatedReviewCount = reviewCountList.get(idList.indexOf(errorPractice.getId())) + 1;
            errorPractice.setReviewCount(updatedReviewCount);
            errorPractice.setReviewTime(new Date());
        }
        boolean b = errorPracticeService.updateBatchById(errorPracticeList);
        ThrowUtils.throwIf(!b, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

}
